package com.yunding.house.utils.wx;


import com.yunding.house.utils.StatusCode;
import net.sf.json.JSONObject;


import java.util.List;

/**
 * ClassName:WeiXinService
 *
 * @Description: 处理微信的事务
 * @Author:chenzhen Date:2019/3/26 20:23 Version 1.0
 **/
public class WeiXinService {

	// 设置APPID
	private static final String APPID = "wxc301b0e710ef1aa5";
	// 设置APPSECRET
	private static final String APPSECRET = "97fb8267b4bdd2ed0806b19feb81c54d";

	 private static String redirect_uri="http://s.zhirui.club/api/getcode/getcode&";
	/**
	 * 处理消息和事件的推送
	 *
	 * @param requestMap
	 * @return
	 */
	/*
	 * public static String getResponse(Map<String, Object> requestMap){ BaseMessage
	 * msg = null; String msgtype = (String) requestMap.get("MsgType");
	 * //MsgType消息的类型 switch (msgtype) { case "text": //文本信息 msg =
	 * dealTextMessage(requestMap); break; case "image": //图片信息 msg =
	 * dealImageMessage(requestMap); break; case "voice": //语言信息
	 *
	 * break; case "event": //事件推送 msg = dealEvent(requestMap); break;
	 *
	 * default: System.out.println("其他业务尚未开放。维护中！"); break; }
	 * //System.out.println(msg); //将class转换为xml推送 if (msg != null) { return
	 * beanToXml(msg); } return null; }
	 *
	 *//**
		 * 实体类转换为XMl
		 *
		 * @param msg
		 * @return
		 *//*
			 * private static String beanToXml(BaseMessage msg) { XStream stream = new
			 * XStream(); stream.processAnnotations(TextMessage.class); //
			 * stream.processAnnotations(ImageMessage.class); //
			 * stream.processAnnotations(MusicMessage.class);
			 * stream.processAnnotations(NewsMessage.class); //
			 * stream.processAnnotations(VideoMessage.class); //
			 * stream.processAnnotations(VoiceMessage.class);
			 *
			 * String ss = stream.toXML(msg); return ss; }
			 */

	/**
	 * 事件的推送
	 *
	 * @param requestMap
	 * @return
	 *//*
		 * private static BaseMessage dealEvent(Map<String, Object> requestMap) {
		 *
		 * String event = (String) requestMap.get("Event"); // 事件类型 switch (event) {
		 * case "CLICK": return dealClick(requestMap);
		 *
		 * case "VIEW": // return dealView(requestMap); break; // 关注事件 case "subscribe":
		 *
		 * List<Article> articles = new ArrayList<>(); String title =
		 * "欢迎关注四川云集家商业管理有限公司"; String description = "立即绑定账号"; String picUrl =
		 * "http://mmbiz.qpic.cn/mmbiz_jpg/qd5nhLiaglj8O70iaiaphj0Tqy57aBMypCQzIW3x4z3l7BcI7daRGdMgCYIUrib8iaYZrvGdtEpYLxC0n0Fc0wwoeAw/0";
		 * //跳转到指定的绑定页面http://www.zhirui.club/login.do String url =
		 * "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID+
		 * "&redirect_uri="+redirect_uri+
		 * "&response_type=code&scope=snsapi_userinfo#wechat_redirect"; articles.add(new
		 * Article(title, description, picUrl, url)); NewsMessage nmsg = new
		 * NewsMessage(); nmsg.setFromUserName((String) requestMap.get("ToUserName"));
		 * nmsg.setToUserName((String) requestMap.get("FromUserName"));
		 * nmsg.setArticles(articles); nmsg.setCreateTime((int)
		 * System.currentTimeMillis() / 1000); nmsg.setMsgType("news");
		 * nmsg.setArticleCount("1"); return nmsg;
		 *
		 * // 取消关注事件 case "unsubscribe":
		 *
		 * break; default: break; } return null; }
		 */

	/**
	 * 单击事件
	 *
	 * @param requestMap
	 * @return
	 */
	/*
	 * private static BaseMessage dealClick(Map<String, Object> requestMap) {
	 *
	 * return null; }
	 *
	 *//**
		 * 单击事件
		 *
		 * @param requestMap
		 * @return
		 */
	/*
	 * private static BaseMessage dealView(Map<String, Object> requestMap) {
	 *
	 * String username = (String) requestMap.get("FromUserName"); // 用户的oppend ID
	 *
	 * return null; }
	 *
	 *//**
		 * 图片信息处理
		 *
		 * @param requestMap
		 * @return
		 */
	/*
	 * private static BaseMessage dealImageMessage(Map<String, Object> requestMap) {
	 *
	 * return null; }
	 *
	 *//**
		 * 文本信息处理
		 *
		 * @param requestMap
		 * @return
		 */
	/*
	 * private static BaseMessage dealTextMessage(Map<String, Object> requestMap) {
	 * String msg = (String) requestMap.get("Content"); // 文本内容 TextMessage textmag
	 * = new TextMessage(); textmag.setFromUserName((String)
	 * requestMap.get("ToUserName")); textmag.setContent("hello,您好！您发送的信息已经接收~");
	 * textmag.setToUserName((String) requestMap.get("FromUserName"));
	 * textmag.setCreateTime((int) System.currentTimeMillis() / 1000);
	 * textmag.setMsgType("text"); return textmag;
	 *
	 * }
	 *
	 *//**
		 * 通过accesstoken和用户的openid获取用户所有信息 当用户点击注册的时候，才进行加载数据
		 *
		 * @param requestMap
		 * @return
		 *//*
			 * public String getXeiXinUser(Map<String, Object> requestMap) { String userurl
			 * =
			 * "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
			 * String openid = (String) requestMap.get("FromUserName"); userurl =
			 * userurl.replace("ACCESS_TOKEN", NetUtils.getAccessToken()).replace("OPENID",
			 * openid); String userinfo = NetUtils.get(userurl); return userinfo; }
			 */

	/**
	 * 返回用户的信息
	 *
	 * @return
	 */
	public static String getAllweinxinUser() {
		String token = NetUtils.getAccessToken();
		String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + token + "&next_openid=";
		String data = NetUtils.get(url);
		return data;
	}

	/**
	 * 获取关注列表
	 *
	 * @return
	 */
	public static List getUserList() {
		String josnstr = getAllweinxinUser();
		JSONObject jsonObject = JSONObject.fromObject(josnstr);
		List data = (List) jsonObject.getJSONObject("data").get("openid");

		return data;

	}

	/**
	 * 获取网页授权的token
	 *
	 * @return
	 */
	public static String getCJToken(String code) {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + APPSECRET
				+ "&code=" + code + "&grant_type=authorization_code";
//		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPID + "&redirect_uri=" + redirect_uri
//				+ "response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";

//		https://open.weixin.qq.com/connect/oauth2/authorize?
//			appid=APPID
//			&redirect_uri=REDIRECT_URI
//			&response_type=code
//			&scope=snsapi_base
//			&state=STATE#wechat_redirect

		System.out.println("url=" + url);
		String data = NetUtils.get(url);
		return data;
	}

	/**
	 * 网页授权 获取用户的基本信息
	 */
	public static String GetSQUser(String token, String openid) {

		String userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openid
				+ "&lang=zh_CN";
		String userdata = NetUtils.get(userurl);
		return userdata;
	}

	/**
	 * 网页授权 获取第二步的refresh_token后，请求以下链接获取access_token：
	 */
	public static String Getrefresh_token(String refreshtoken) {

		String userurl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + APPID + "&grant_type=refresh_token&refresh_token=" + refreshtoken;
		String userdata = NetUtils.get(userurl);
		JSONObject json = JSONObject.fromObject(userdata);

//		String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("APPID", appid);
//		params.put("REFRESH_TOKEN", refresh_token);
//
//		JSONObject json = HttpUtil.get(StringFormat.format(url, params));
		if(json != null){
		    boolean isErr = json.containsKey("errcode");
		    if(isErr){//请求失败
		        String errCode = json.getString("errcode");
		        String errMsg = json.getString("errmsg");
		        System.out.println(json);
		        return errCode;
		    }else{
		        String accessToken = json.getString("access_token");
		        int expires = json.getInt("expires_in");
		        String refreshToken = json.getString("refresh_token");
		        String openid = json.getString("openid");
		        String scope = json.getString("scope");
		        return userdata;
		    }
		}
		return StatusCode.FAIL;
	}


	/**
	 * 群发文本
	 *
	 * @param data
	 * @return
	 */
	public static String qunfa(String data) {

		String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + NetUtils.getAccessToken();
		String post = NetUtils.post(url, data);
		return post;
	}

	/**
	 * 模板消息发送
	 *
	 * @return
	 */
	/*
	 * public static String tuiMoBan(String openid, Take take, String mobanurl) {
	 * String url =
	 * "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +
	 * NetUtils.getAccessToken(); String template =
	 * "2QbXBzJKD_4Mn-FIqXd3ieqCPIMAxyfYHWonXo7B8bA";
	 *
	 * TakeTemplate ttl = new TakeTemplate(openid, template, mobanurl, take);
	 * JSONObject json = JSONObject.fromObject(ttl);
	 *
	 * String post = NetUtils.post(url, json.toString()); System.out.println(post);
	 * return post; }
	 */

}
